<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>队列</title>
</head>

<body>
    <script type="module">
        import { Queue, PriorityQueue } from '../structure/queue/queue.js'

        const queue = new Queue()

        queue.enqueue('a')
        queue.enqueue('b')
        queue.enqueue('c')
        queue.enqueue('d')

        console.log('当前队列元素：', queue.items)
        console.log('出队列：', queue.dequeue())
        console.log('当前队列首部元素：', queue.front())
        console.log('当前队列元素：', queue.items)

        // 新版击鼓传花：几个人围成一圈, 数数, 数到固定数的人淘汰, 问最后剩谁
        /*
            思路：以 why, tom, lilei, lucy 为例, 3
                why1  tom2  lilei3(淘汰)
                lucy1  why2  tom3(淘汰)
                lucy1  why2  lucy3(淘汰)
                why 结束 
            过程：首先入队列, 每次拿出队列首部元素放到尾部  
        */
        function passGame(nameList, num) {
            // 创建队列
            const queue = new Queue()
            for (let i = 0; i < nameList.length; i++) {
                queue.enqueue(nameList[i])
            }
            // 循环让这些人进入到队列中
            while (queue.size() > 1) {
                for (let i = 0; i < num - 1; i++) {
                    queue.enqueue(queue.dequeue())
                }
                queue.dequeue()
            }
            return queue.front()
        }

        console.log(passGame(['why', 'tom', 'lilei', 'lucy'], 3))

        // 测试优先级队列
        const queue_pr = new PriorityQueue()

        queue_pr.enqueue('aaa', 100)
        queue_pr.enqueue('bbb', 150)
        queue_pr.enqueue('ccc', 120)
        queue_pr.enqueue('ddd', 90)

        console.log(queue_pr.items)

        console.log('出队列', queue_pr.dequeue())
    </script>
</body>

</html>